<template>
  <div class="login-container">
    <NavBar />
    <br>
    <el-tabs v-model="activeTab" type="card">
      <el-tab-pane label="登录" name="login">
        <el-form @submit.native.prevent="login">
          <el-form-item>
            <el-input v-model="loginForm.username" placeholder="用户名" required></el-input>
          </el-form-item>
          <el-form-item>
            <el-input type="password" v-model="loginForm.password" placeholder="密码" required></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" native-type="submit">登录</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="注册" name="register">
        <el-form :model="registerForm" status-icon @submit.native.prevent="handleRegister">
          <el-form-item label="用户名">
            <el-input v-model="registerForm.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input type="password" v-model="registerForm.password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="确认密码">
            <el-input type="password" v-model="registerForm.confirmPassword" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" native-type="submit">注册</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>

export default {
  data() {
    return {
      activeTab: 'login',
      loginForm: {
        username: '',
        password: ''
      },
      registerForm: {
        username: '',
        password: '',
        confirmPassword: ''
      }
    };
  },
  methods: {
    login() {
      // 登录逻辑
      localStorage.setItem('user', 'true');
      localStorage.setItem('username', this.loginForm.username);
      this.$router.push('/profile');
    },
    handleRegister() {
      if (this.registerForm.password !== this.registerForm.confirmPassword) {
        this.$message.error('两次输入的密码不一致');
        return;
      }
      // 假设注册逻辑成功
      console.log('注册信息:', this.registerForm);
      // 注册成功后跳转到登录页面
      this.activeTab = 'login';
    }
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh; /* 使容器占满整个视口高度 */
  background-color: #f0f2f5; /* 背景颜色 */
}

.el-tabs {
  width: 400px; /* 设置表单宽度 */
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.el-tabs h2 {
  text-align: center;
  margin-bottom: 20px;
  color: #333;
}

.el-form-item {
  margin-bottom: 15px;
}

.el-input {
  width: 100%;
}

.el-button {
  width: 100%;
  margin-top: 10px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-container {
    padding: 20px; /* 在小屏幕上添加内边距 */
  }
  .el-tabs {
    width: 100%; /* 在小屏幕上使表单宽度为100% */
  }
}
</style>